package com.kwz.common.ui.events;

import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import com.kwz.common.IContext;
import com.kwz.common.designpatterns.factory.BadContextException;
import com.kwz.common.designpatterns.functor.IFunctorEx;

/**
 * <p>
 * 
 * </p> 
 *
 * @author Ccr
 */
public class UiFocusListener extends UiFunctorListener implements FocusListener {

    /**
     * @throws BadContextException
     */
    public UiFocusListener() throws BadContextException {
        this(null);
    }

    /**
     * @param context
     * @throws BadContextException
     */
    public UiFocusListener(IContext context) throws BadContextException {
        super(context);
    }

    // ------------------------- Interface FocusListener ------------------
    /* (non-Javadoc)
     * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
     */
    public void focusGained(final FocusEvent e) {
        UiBaseEventExec exec = new UiBaseEventExec(){

            public void prepareCommand(IFunctorEx functor) throws Exception {
                Component previous = e.getOppositeComponent();
                functor.arg(previous);
            }
            
        };
        exec.processEvent(UI_ON_FOCUS_GAINED, e);
    }

    /* (non-Javadoc)
     * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
     */
    public void focusLost(final FocusEvent e) {
        UiBaseEventExec exec = new UiBaseEventExec(){

            public void prepareCommand(IFunctorEx functor) throws Exception {
                Component next = e.getOppositeComponent();
                functor.arg(next);
            }
            
        };
        exec.processEvent(UI_ON_FOCUS_LOST, e);
    }

}
